add_rules("mode.debug", "mode.release")
-- add_rules("mode.debug")

--  设置工具链 xmake f -p cross --sdk=C:/arm-none-eabi --toolchain=gnu-rm
--   配置vscode的intellisense自动提醒等功能 xmake project -k compile_commands
set_config("sdk","C:/arm-none-eabi")
set_config("toolchain","gnu-rm")
set_config("plat","cross")
-- set_languages("c99", "cxx11")

task("flash")
    on_run(function () 
            print("**********************以下开始写入nrf52*******************")
            os.exec([[openocd -f interface/cmsis-dap.cfg -f target/stm32f1x.cfg -c init -c halt -c "program build/1.hex verify reset exit" ]])
            print("******************如果上面没有error,则写入成功********************")
        end)

target("stm32f103")
    set_kind("binary")
    add_files(
		"./Core/Src/*.c",
		"./lib/u8g2/*.c",
        "./startup_stm32f103xb.S",
        "./Drivers/STM32F1xx_HAL_Driver/Src/*.c"
        )
    del_files(
        "./Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_timebase_rtc_alarm_template.c",
        "./Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_timebase_tim_template.c",
        "./Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_msp_template.c"
        )
    add_includedirs(
        "./Core/Inc",
        "./Drivers/CMSIS/Include",
        "./lib/u8g2",
        "./Drivers/CMSIS/Device/ST/STM32F1xx/Include",
        "./Drivers/STM32F1xx_HAL_Driver/Inc",
        "./Drivers/STM32F1xx_HAL_Driver/Inc/Legacy"
        )
    add_defines(
        "USE_HAL_DRIVER",
        "STM32F103xB"
    )
    

    add_cflags(
        "-O3 -g3",
        "-mcpu=cortex-m3",
        "-mthumb",
        "-Wall -fdata-sections -ffunction-sections",
        "-g -gdwarf-2"
        )
    add_asflags(
        "-O3 -g3",
        "-mcpu=cortex-m3",
        "-mthumb",
        "-x assembler-with-cpp",
        "-Wall -fdata-sections -ffunction-sections",
        "-g -gdwarf-2"
        )

    add_ldflags(
        "-O3 -g3",
        "-mcpu=cortex-m3",
        "-L./",
        "-TSTM32F103C8Tx_FLASH.ld",
        "-Wl,--gc-sections",
        "-lc -lm -lnosys -lrdimon -u _printf_float",{force = true}
        )
    set_targetdir("build")
    set_filename("1.elf")

    after_build(function(target)
        print("生成HEX 和BIN 文件")
        os.exec("arm-none-eabi-objcopy -O ihex .//build//1.elf .//build//1.hex")
        os.exec("arm-none-eabi-objcopy -O binary .//build//1.elf .//build//1.bin")
        print("生成已完成")
        import("core.project.task")
        task.run("flash")
        print("********************储存空间占用情况*****************************")
        os.exec("arm-none-eabi-size -Ax .//build//1.elf")
        os.exec("arm-none-eabi-size -Bx .//build//1.elf")
        os.exec("arm-none-eabi-size -Bd .//build//1.elf")
        print("heap-堆、stck-栈、.data-已初始化的变量全局/静态变量，bss-未初始化的data、.text-代码和常量")
    end)

